package com.njzxw.fs.commonutil.web;


import com.njzxw.fs.commonutil.bean.Common;
import com.njzxw.fs.commonutil.util.GsonUtil;
import com.njzxw.fs.commonutil.util.StringHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class BaseController {

    @Autowired
    private StringHelper stringHelper;

    @Autowired
    protected Common common;

    public org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 写入客户端
     *
     * @param obj
     *
     * @author 雷杨
     * @date 2013-5-18
     */
    public void writeText(Object obj, HttpServletResponse response) {
        try {
//            if (common.isTest()) {
//                if (!(obj instanceof Integer) && !(obj instanceof String) && !(obj instanceof Long) && !(obj instanceof Float)) {
//                    logger.error("返回writeText参数值错误，只能返回对应 Integer、String、Long、Float类型");
//                    response.getWriter().print("");
//                    return;
//                }
//            }
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(obj);
        } catch (Exception e) {
            // e.printStackTrace();
            logger.debug("写入文本返回失败：", e);
        }
    }

    public void writeJsonDataNoNull(ReturnMap returnMap, HttpServletResponse response) {
        try {

            Map data = null;
            if (returnMap != null) {
                data = returnMap.getReturnMap();
            }

            if (response.getHeader("Access-Control-Allow-Credentials") == null) {
                response.setContentType("application/json;charset=utf-8");
            }
            response.setCharacterEncoding("utf-8");

            JsonConfig jsonConfig = new JsonConfig();
            PropertyFilter filter = new PropertyFilter() {
                public boolean apply(Object object, String fieldName, Object fieldValue) {
                    return null == fieldValue;
                }
            };
            jsonConfig.setJsonPropertyFilter(filter);

            if (data instanceof Map) {
                response.getWriter().print(JSONObject.fromObject(data,jsonConfig));
            } else if (data instanceof List) {
                response.getWriter().print(JSONArray.fromObject(((List) data),jsonConfig));
            } else {
                response.getWriter().print(JSONObject.fromObject(data,jsonConfig));
            }
        } catch (Exception e) {
            logger.debug("写入json返回失败：", e);
        }
    }

    public void writeJsonData(ReturnMap returnMap, HttpServletResponse response) {
        try {
            Map data = null;
            if (returnMap != null) {
                data = returnMap.getReturnMap();
            }

            if (response.getHeader("Access-Control-Allow-Credentials") == null) {
                response.setContentType("application/json;charset=utf-8");
            }
            response.setCharacterEncoding("utf-8");

            response.getWriter().print(GsonUtil.parseObjectToJsonString(data));
        } catch (Exception e) {
            logger.debug("写入json返回失败：", e);
        }
    }

    public void writeHtml(String data, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(data);
        } catch (Exception e) {
            logger.debug("写入json返回失败：", e);
        }
    }

}
